package demo;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ActionServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(ActionServlet.class
			.getName());

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		String action = req.getParameter("action");
		String uid = req.getParameter("uid");
		String role = req.getParameter("role");
		UserAO ao = new UserAO();
		PrintWriter out = resp.getWriter();

		if (action.equals("Delete")) {

			if (uid != null || uid.trim().length() != 0) {

				if (role.equalsIgnoreCase("admin") == true) {

					out.println("<html><body onLoad=\"alert('Invalid request');window.location.href='http://omnihtrack.appspot.com/userlist.html';\"></html>");

				} else {
					Long id = Long.valueOf(uid);
					ao.deleteUser(id);

					out.println("<html><body onLoad=\"alert('User has been successfully deleted');window.location.href='http://omnihtrack.appspot.com/userlist.html';\"></html>");

				}
			}
		} else if (action.equals("Update")) {

			if (uid != null || uid.trim().length() != 0) {

				if (role.equalsIgnoreCase("admin") == true) {

					out.println("<html><body onLoad=\"alert('Invalid request');window.location.href='http://omnihtrack.appspot.com/userlist.html';\"></html>");

				} else {

					Long id = Long.valueOf(uid);
					String trackername = req.getParameter("trackername");
					String password = req.getParameter("password");
					String simno = req.getParameter("simno");
					String imei = req.getParameter("imei");
					String typeoftracker = req.getParameter("typeoftracker");
					String phoneno = req.getParameter("phoneno");
					String passcode = req.getParameter("passcode");

					if (password.trim().length() < 4
							|| simno.trim().length() < 8
							|| phoneno.trim().length() < 8) {
						out.println("<html><body onLoad=\"alert('Field not fullfilled. Please re-enter information');window.location.href='http://omnihtrack.appspot.com/userlist.html';\"></html>");

					} else {

						String testNumber = password + simno + phoneno;
						boolean onlyNumber = ao.containsDigit(testNumber);

						boolean validsim = ao.validSim(simno);
						boolean validphone = ao.validphone(phoneno);
						
						if (onlyNumber == false || validsim == false
								|| validphone == false) {
							out.println("<html><body onLoad=\"alert('Invalid number field entered. Please re-enter information');window.location.href='http://omnihtrack.appspot.com/userlist.html';\"></html>");
						} else {

							ao.updateUser(id, trackername, password, simno,
									imei, typeoftracker, phoneno, passcode,
									role);

							out.println("<html><body onLoad=\"alert('User has been successfully updated');window.location.href='http://omnihtrack.appspot.com/userlist.html';\"></html>");

						}
					}
				}
			}
		} else {

			out.println("<html><body onLoad=\"alert('Error');window.location.href='http://omnihtrack.appspot.com/userlist.html';\"></html>");

		}
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		doGet(req, resp);
	}

}
